Skip to content

fix: avoid mime_content_type warning on wrapped streams - #592

Merged
janepie merged 2 commits into
nextcloud:mainfrom
olddude2:fix/mime-content-type-stream-cast-warning
Aug 4, 2026
Merged

fix: avoid mime_content_type warning on wrapped streams#592
janepie merged 2 commits into
nextcloud:mainfrom
olddude2:fix/mime-content-type-stream-cast-warning

Conversation

@olddude2

Copy link
Copy Markdown
Contributor

mime_content_type() requires stream_cast() support to operate on a stream resource directly. Streams returned by File::fopen() can be wrapped in Icewind\Streams\CallbackWrapper, which does not implement stream_cast(), causing a PHP warning to be logged on every preview/output-file request even though the mime type is still detected correctly.

This drains the stream into a temporary file first, then calls mime_content_type() against the file path instead. No behavior change; only removes log noise.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

mime_content_type() requires stream_cast() support to operate on a
stream resource directly. Streams returned by File::fopen() can be
wrapped in Icewind\Streams\CallbackWrapper, which does not implement
stream_cast(), causing a PHP warning to be logged on every
preview/output-file request even though the mime type is still
detected correctly.

This drains the stream into a temporary file first, then calls
mime_content_type() against the file path instead. No behavior change;
only removes log noise.

Signed-off-by: Simon Holzman <simon.holzman@verizon.net>
@olddude2

olddude2 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

The PHPUnit / php8.2-sqlite-master failure appears to be a pre-existing
issue — the job fails during Nextcloud installation before any tests run,
with "This version of Nextcloud requires at least PHP 8.3" (PHP 8.2.31 is
being used against master). The php8.2-sqlite-stable33 job passes cleanly.

Happy to rebase or make any adjustments if needed — just let me know.

Comment thread lib/Service/AssistantService.php Outdated
public function getOutputFilePreviewFile(string $userId, int $taskId, int $fileId, ?int $x = 100, ?int $y = 100): ?array {
$taskOutputFile = $this->getTaskOutputFile($userId, $taskId, $fileId);
$realMime = mime_content_type($taskOutputFile->fopen('rb'));
$realMime = $this->detectMimeType($taskOutputFile->fopen('rb'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$realMime = $this->detectMimeType($taskOutputFile->fopen('rb'));
$head = fread($stream, 4096);
if (is_resource($stream)) {
fclose($stream);
}
$realMime = new \finfo(FILEINFO_MIME_TYPE)->buffer($head);

Thank you for your contribution! Unfortunately we cannot merge this as copying the whole file to disk has a too high performance impact especially for big files. An easier fix would be to not use mime_content_type as shown above, would you be up for testing and validating if that works without any issues?

@janepie

janepie commented Jul 13, 2026

Copy link
Copy Markdown
Member

fixes #591

Avoids copying the whole file to disk for mime detection.
Reads only the first 4096 bytes into a buffer and uses
finfo::buffer() to detect the mime type, which does not
require stream_cast() support.

Addresses maintainer feedback on nextcloud#592.

Signed-off-by: Simon Holzman <simon.holzman@verizon.net>
@olddude2

Copy link
Copy Markdown
Contributor Author

Hi — just checking in on this whenever you get a chance. I updated the fix per the earlier feedback to use finfo::buffer() on a small read instead of the temp-file approach. Let me know if anything else needs adjusting. Thanks for your time on this. Cheers!

@janepie
janepie self-requested a review August 4, 2026 14:00

@janepie janepie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for coming back on this so late. Looks good, will be included in the NC35 release!

@janepie
janepie merged commit cad5a7f into nextcloud:main Aug 4, 2026
14 checks passed
@janepie janepie mentioned this pull request Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mime_content_type() logs a stream_cast warning on every Assistant preview/output-file request

2 participants